package com.zsix.system.strategy.userByTaskRule;

import com.zsix.common.util.StringUtils;
import com.zsix.system.mapper.orgmanagementMapper.UserEntityMapper;
import com.zsix.system.service.IAssigneeUserService;
import com.zsix.common.entity.orgmanagement.UserEntity;
import com.zsix.common.entity.taskAssignee.AssigneeDSREntity;
import com.zsix.common.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 按照绑定的部门与岗位交集计算节点处理人，多人处理模式
 */
@Component
public class ByDeptAndPositionList implements UserByTaskRuleStrategy {
    @Autowired
    private IAssigneeUserService assigneeUserService;
    @Autowired
    private UserEntityMapper userEntityMapper;
    @Autowired
    private RedisUtils redisUtils;
    public HashMap<String,Object> getUserByTaskRule(String taskid, String ruleName
            , HashMap<String,Object> variables, String procInstId) {
        //获取设置的接收人数据
        HashMap<String, Object> hashMap = assigneeUserService
                .getAssigneeUserWithTaskKey(taskid, ruleName);
        List<AssigneeDSREntity> assigneeDSREntityList = (List<AssigneeDSREntity>) hashMap.get("assigneeDSREntities");
        String nextWorkEmp = "";
        List<UserEntity> deptUserEntityList = new ArrayList<>();
        List<UserEntity> stionUserEntityList = new ArrayList<>();
        List<String> byDeptAndPositionList = new ArrayList<>();
        for (AssigneeDSREntity ae : assigneeDSREntityList) {

            if (ae.getDsr().equals("dept")) {
                deptUserEntityList = userEntityMapper.getUserByDepartId(ae.getTargetId());
            }
            if (ae.getDsr().equals("position")) {
                List<UserEntity> userEntityList = userEntityMapper.getUserByPositionId(ae.getTargetId());
                for (UserEntity userEntity : userEntityList) {
                    stionUserEntityList.add(userEntity);
                }
            }

        }
        for (UserEntity deptUser : deptUserEntityList) {
            for (UserEntity staionuser : stionUserEntityList) {
                if (deptUser.getUsername().equals(staionuser.getUsername())) {
                    if (nextWorkEmp.contains(deptUser.getUsername() + ","))
                        continue;
                    byDeptAndPositionList.add(deptUser.getUsername());
                    nextWorkEmp +=  deptUser.getUsername() + ",";
                }
            }
        }

        if(!StringUtils.isNull(nextWorkEmp)){
            nextWorkEmp = nextWorkEmp.substring(0, nextWorkEmp.length() - 1);
        }

        //放入缓存，用于发送消息推送
        variables.put(taskid + "_byDeptAndPositionList", byDeptAndPositionList);
        if(redisUtils.exists(procInstId+"_sms")){
            redisUtils.remove(procInstId+"_sms");
        }
        redisUtils.set(procInstId+"_sms",nextWorkEmp);
        return variables;
    }
    @Override
    public String getType(){
        return "byDeptAndPositionList";
    }
}
